﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DataConsistency.UC
{
    public partial class MatKetNoi : UserControl
    {
        public int SoLuongMU = 80;
        public double xacSuatMatKetNoi;
        public int ThoiGianMatKetNoi;
        Para CPara = new Para();
        public MatKetNoi()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MatKetNoi_Load(object sender, EventArgs e)
        {
            //SoLuongMU = int.Parse(txtSoluongMU.Text);
            xacSuatMatKetNoi = double.Parse(txtXacSuat.Text);
            ThoiGianMatKetNoi = int.Parse(txtThoigian.Text);
            //MessageBox.Show(SoLuongMU.ToString() + " " + xacSuatMatKetNoi.ToString() + " " + ThoiGianMatKetNoi.ToString()); 
        }

        public bool checkInList(List<int> list, int x)
        {
            bool flag = false;
            for (int i = 0; i < list.Count; i++)
            {
                if (x == list[i])
                    flag = true;
            }
            return flag;
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnXacSuat_Click(object sender, EventArgs e)
        {
            //xacSuatMatKetNoi = double.Parse(txtXacSuat.Text);
            //int soMUMatKetNoi = (int)(xacSuatMatKetNoi * SoLuongMU);
            int soMUMatKetNoi= 10;

            List<int> lIdMU = new List<int>();
            List<int> lTH = new List<int>();

            Random rand = new Random();
            string XacSuatOutPut = soMUMatKetNoi.ToString() + "\r\n";

            // Random MU Mat ket noi
            for (int i = 0; i < soMUMatKetNoi; i++)
            {
                int MURand;
                
                // Check MU co trong list ko
                do
                {
                    MURand = rand.Next(1, SoLuongMU);
                }
                while (checkInList(lIdMU, MURand) == true);
                lIdMU.Add(MURand);
                
            }
            // Random truong hop mat ket noi
            for (int j = 0; j < soMUMatKetNoi; j++)
            {
                int th = rand.Next(1, 10);
                lTH.Add(th);
            }

            // Xuat Out put
            for (int k = 0; k < soMUMatKetNoi; k++)
            {
                //XacSuatOutPut = XacSuatOutPut + "MU" + lIdMU[k].ToString() + " (" + lTH[k].ToString() + ")  " + "\r\n";
                XacSuatOutPut = XacSuatOutPut + "MU" + lIdMU[k].ToString() + "\r\n";
            }
            

            //Ghi ra file
            SaveFileDialog DialogSave = new SaveFileDialog();
            DialogSave.DefaultExt = "txt";
            DialogSave.Filter = "TXT file (*.txt)|*.txt";
            DialogSave.AddExtension = false;
            DialogSave.RestoreDirectory = true;
            DialogSave.Title = "Where do you want to save the file?";
            DialogSave.InitialDirectory = @"C:/cygwin/usr/local/ns-allinone-2.31/ns-2.31";
            // Show the dialog and process the result
            if (DialogSave.ShowDialog() == DialogResult.OK)
            {
                string writeFile = DialogSave.FileName;
                CPara.ClearFile(writeFile);
                CPara.WriteFile(writeFile,XacSuatOutPut);

            }
            DialogSave.Dispose();
            DialogSave = null;
        }

        private void btnThoiGian_Click(object sender, EventArgs e)
        {
            //RanDomThoiGianMatKetNoi();
                        
            //Random thoi gian mat ket noi theo so MU 
            int start = 0;
            int end = 0;
            int chuky = int.Parse(txtChuKy.Text);
            int soluong = int.Parse(txtThoigian.Text);
            string OutputStr = "";
            //$ns_ at 800.0 "$p_(8) setDisconnect 12 23"   "\""
            Random rand = new Random();
            for (int i = 1; i <= soluong; i++)
            {
                int sogiay = rand.Next(1, 15);
                // Random thoi gian bat dau
                start = rand.Next(1, 30);
                end = start + sogiay;

                OutputStr = OutputStr + "$ns_ at 800.0 " + "\"" + "$p_() setDisconnect " + start.ToString() + " " + end.ToString() + "\"" + "\r\n";

            }

            //Ghi ra file
            SaveFileDialog DialogSave = new SaveFileDialog();
            DialogSave.DefaultExt = "txt";
            DialogSave.Filter = "TXT file (*.txt)|*.txt";
            DialogSave.AddExtension = false;
            DialogSave.RestoreDirectory = true;
            DialogSave.Title = "Where do you want to save the file?";
            DialogSave.InitialDirectory = @"C:/cygwin/usr/local/ns-allinone-2.31/ns-2.31";
            // Show the dialog and process the result
            if (DialogSave.ShowDialog() == DialogResult.OK)
            {
                string writeFile = DialogSave.FileName;
                CPara.ClearFile(writeFile);
                CPara.WriteFile(writeFile, OutputStr);

            }
            DialogSave.Dispose();
            DialogSave = null;
        }

        /// <summary>
        /// Random thoi gian mat ket noi old
        /// </summary>
        public void RanDomThoiGianMatKetNoi()
        {
            // Ran dom thoi gian mat ket noi 
            int start = 0;
            int end = 0;
            int chuky = int.Parse(txtChuKy.Text);
            int tgmatketnoi = int.Parse(txtThoigian.Text);

            Random rand = new Random();

            start = rand.Next(1, chuky - tgmatketnoi - 1);
            end = start + tgmatketnoi;

            string Output = "Chu ky: " + chuky.ToString() + "\r\n";
            Output = Output + "Thoi gian mat ket noi: " + tgmatketnoi.ToString() + "\r\n";
            Output = Output + "Bat dau: " + start.ToString() + "\r\n";
            Output = Output + "Ket thu: " + end.ToString() + "\r\n";

            //Ghi ra file
            SaveFileDialog DialogSave = new SaveFileDialog();
            DialogSave.DefaultExt = "txt";
            DialogSave.Filter = "TXT file (*.txt)|*.txt";
            DialogSave.AddExtension = false;
            DialogSave.RestoreDirectory = true;
            DialogSave.Title = "Where do you want to save the file?";
            DialogSave.InitialDirectory = @"C:/cygwin/usr/local/ns-allinone-2.31/ns-2.31";
            // Show the dialog and process the result
            if (DialogSave.ShowDialog() == DialogResult.OK)
            {
                string writeFile = DialogSave.FileName;
                CPara.ClearFile(writeFile);
                CPara.WriteFile(writeFile, Output);

            }
            DialogSave.Dispose();
            DialogSave = null;
        }
    }
}
